<?php

namespace app\customer\logic;
use app\data\util\HttpUtil;
use think\facade\Db;
class EmailTagLogic extends BasicLogic
{

    /**
     * @desc   获取标签新增列表
     * @author luckyXiong
     * @date   2025-01-24
     */
    public function addTagList($params=[]){
        $create_time             = getIsset($params,'create_time');
        $shop_id                 = getIsset($params,'shop_id');
        $limit                   = getIsset($params,'limit',10);
        $where = [];
        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        if($shop_id){
            $where[]  = ['shop_id', '=',$shop_id];
        }

        $order = ['create_time desc'];
        $res   = Db::name('customer_email_add')
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];
        $shop_ids       = array_column($list,'shop_id');
        $shop_names     = Db::name('shops')->field('id,name,myshopify_domain')->whereIn('id',$shop_ids)->select()->toArray();
        $shop_names_map = array_column($shop_names,null,'id');
        //批量获取创建人;
        $operater_ids    = array_column($list,'operate_id');
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        foreach($list as $k=>$v){
            $list[$k]['date_zone'] = $v['date_zone_start'].'至'.$v['date_zone_end'];
            $shopNames             = $shop_names_map[$v['shop_id']]??[];
            $shop_name             = $shopNames['name']??'';
            $list[$k]['shop_name'] = $shop_name;
            $admin_users_info      = $admin_users_map[$v['operate_id']]??'';
            $list[$k]['operater']  = $admin_users_info['username']??'';
        }

        $data = [
            'list'                         => $list,
            'total'                        => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'操作成功',$data);
    }

    /**
     * @desc   获取标签新增
     * @author luckyXiong
     * @date   2025-01-24
     */
    public function addTag($params=[]){
        $shop_id         = $params['shop_id']??'';
        $tag             = $params['tag']??'';
        $nums            = $params['nums']??'600';
        $date_zone_start = $params['date_zone_start']??'';
        $date_zone_end   = $params['date_zone_end']??'';
        $operate_id      = $params['operate_id']??'';
        if($date_zone_end<$date_zone_start){
            return  $this->jsonMessage(201,false,'结束时间必须大于开始时间!',null);
        }

        //获取需要打标签的数量;
        if($tag=='ALL'){
            $nums = $nums * 14;
        }else{
            $nums = $nums * 1;
        }

        $update_id = Db::name('customer_email_add')->insertGetId(
            [
                'date_zone_start'=>$date_zone_start,
                'date_zone_end'  =>$date_zone_end,
                'tag'            =>$tag,
                'nums'           =>$nums,
                'shop_id'        =>$shop_id,
                'operate_id'     =>$operate_id,
                'update_time'    =>date("Y-m-d H:i:s", time()),
                'create_time'    =>date("Y-m-d H:i:s", time()),
            ]
        );

        // todo 发送消息数据处理
        $url               = Config('Logisticskey.999_url').'/api/check/add_tag';
        $info              = [
            'date_zone_start'=>$date_zone_start,
            'date_zone_end'  =>$date_zone_end,
            'tag'            =>$tag,
            'update_id'      =>$update_id,
            'shop_id'        =>$shop_id,
            'nums'           =>$nums,
        ];
        $res               = HttpUtil::postDataCurl($url, $info);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }


    /**
     * @desc   获取标签删除任务
     * @author luckyXiong
     * @date   2025-01-24
     */
    public function addDelTag($params=[]){
        $shop_id         = $params['shop_id']??'';
        $tag             = $params['tag']??'';
        $operate_id      = $params['operate_id']??'';
        $update_id       = Db::name('customer_email_del')->insertGetId(
            [
                'tag'            =>$tag,
                'shop_id'        =>$shop_id,
                'operate_id'     =>$operate_id,
                'update_time'    =>date("Y-m-d H:i:s", time()),
                'create_time'    =>date("Y-m-d H:i:s", time()),
            ]
        );
        // todo 发送消息数据处理
        $url               = Config('Logisticskey.999_url').'/api/check/del_tag';
        $info              = [
            'tag'            =>$tag,
            'update_id'      =>$update_id,
            'shop_id'        =>$shop_id,
        ];
        $res               = HttpUtil::postDataCurl($url, $info);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }


    /**
     * @desc   获取删除任务列表接口
     * @author luckyXiong
     * @date   2025-01-24
     */
    public function delTagList($params=[]){
        $create_time             = getIsset($params,'create_time');
        $shop_id                 = getIsset($params,'shop_id');
        $limit                   = getIsset($params,'limit',10);
        $where = [];
        if ($create_time) {
            $where[]  = ['create_time', '>=', date('Y-m-d H:i:s',strtotime($create_time[0]))];
            $where[]  = ['create_time', '<=', date('Y-m-d H:i:s',strtotime($create_time[1]) + 3600*24-1)];
        }

        if($shop_id){
            $where[]  = ['shop_id', '=',$shop_id];
        }

        $order = ['create_time desc'];
        $res   = Db::name('customer_email_del')
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $list = $res->items()?:[];
        $shop_ids       = array_column($list,'shop_id');
        $shop_names     = Db::name('shops')->field('id,name,myshopify_domain')->whereIn('id',$shop_ids)->select()->toArray();
        $shop_names_map = array_column($shop_names,null,'id');
        //批量获取创建人;
        $operater_ids    = array_column($list,'operate_id');
        $admin_users     = Db::name('system_admin')->field('id,username')->whereIn('id',$operater_ids)->select()->toArray();
        $admin_users_map = array_column($admin_users,null,'id');
        foreach($list as $k=>$v){
            $shopNames             = $shop_names_map[$v['shop_id']]??[];
            $shop_name             = $shopNames['name']??'';
            $list[$k]['shop_name'] = $shop_name;
            $admin_users_info      = $admin_users_map[$v['operate_id']]??'';
            $list[$k]['operater']  = $admin_users_info['username']??'';
        }

        $data = [
            'list'                         => $list,
            'total'                        => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'操作成功',$data);
    }


    /**
     * @desc   获取标签的下来列表
     * @author luckyXiong
     * @date   2025-01-24
     */
    public function getTagList($params=[]){
         $list = Config('Logisticskey.emial_tags');
         array_unshift($list,'ALL');
        $info        = getLevel($list);
        foreach($info as $k=>$v){
            $arr[] = [
                'value'      => $v['key']+1,
                'label'      => $v['value'],
            ];
        }
        return  $this->jsonMessage(200,true,'获取成功',$arr);
    }





}